<template>
	<view class="content u-cb-white-bg plr-100 pt-100">
		<view class="u-text-align-center fsize-34">修改密码</view>
		<view class="mt-100">
			<u-form 
				:model="form" 
				:rules="rules"
				ref="uForm"
				:errorType="['toast']">
				<u-form-item prop="password">
					<u-input v-model="form.password" :password="isShowPassword" :clearable="true" border="bottom" placeholder="原密码" >
						<template slot="suffix">
							<u-icon 
								size="20" 
								:name="isShowPassword?'eye-fill':'eye'" 
								:color="isShowPassword?'#c0c4cc':'#F56C6C'" 
								@click="isShowPassword=!isShowPassword" />
						</template>
					</u-input>
				</u-form-item>
				<u-form-item prop="new_password">
					<u-input 
						:password="isShowNewPassword"
						v-model="form.new_password" 
						:clearable="true" 
						border="bottom" 
						placeholder="新密码">
							<template slot="suffix">
								<u-icon 
									size="20" 
									:name="isShowNewPassword?'eye-fill':'eye'" 
									:color="isShowNewPassword?'#c0c4cc':'#F56C6C'" 
									@click="isShowNewPassword=!isShowNewPassword" />
							</template>
					</u-input>
				</u-form-item>
			</u-form>
			<view class="mt-40">
				<u-button @click="submit" type="error" text="确认修改" />
			</view>
		</view>
	</view>
</template>

<script>
	import {mapState, mapMutations} from 'vuex'
	export default{
		data(){
			return {
				currentIndex: 0,
				isShowNewPassword: true,
				isShowPassword: true,
				form:{
					new_password:'',
					password:'',
				},
				pwType: 1, // 1商家修改 2会员修修改
				rules: {
					new_password: [
						{ required: true, message: '新密码不能为空', trigger: 'change'},
						{ 
							validator:(rule,value,callback)=>{ return uni.$u.test.enOrNum(value)},
							message:'密码只能包含字母或数字', 
							trigger: ['change','blur'],
						}
					],
					password: [
						{ required: true, message: '原密码不能为空', trigger: 'change'},
					]
				}
			}
		},
		onReady() {
			// 如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则
			this.$refs.uForm.setRules(this.rules)
		},
		onLoad(option) {
			this.pwType = parseInt(option.type)
		},
		methods: {
			...mapMutations(['logout']),
			submit() {
				let that = this
				this.$refs.uForm.validate().then(res => {
					if(that.pwType === 1){
						that.$reqPost('/SetUserPassword',that.form).then(res=>{
							this.logout()
							uni.reLaunch({url:'/pages/login/index'})
						})
					}else if(that.pwType === 2) {
						that.$reqPost('/SetMemberPassword',that.form).then(res=>{
							this.logout()
							uni.reLaunch({url:'/pages/login/index'})
						})
					}
				}).catch(errors => {
					// uni.$u.toast('校验失败')
				})
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content{
		height:100vh;
	}
</style>